<template>
  <div>
    <iep-page-header title="收货地址" :backOption="backOption"></iep-page-header>
    <div class="opera" style="margin-bottom: 10px">
      <el-button type="warning" @click="handleCreate()">新增</el-button>
    </div>
    <iep-table
      :isLoadTable="isLoadTable"
      :pagination="pagination"
      :dictsMap="dictsMap"
      :columnsMap="columnsMap"
      :pagedTable="pagedTable"
      @pagination-size-change="handlePaginationSizeChange"
      @pagination-current-change="handlePaginationCurrentChange"
      :isPagination="false"
    >
      <el-table-column prop="operation" label="操作" width="220" align="center">
        <template slot-scope="scope">
          <operation-wrapper>
            <iep-button type="warning" plain @click="handleUpdate(scope.row)">修改</iep-button>
            <iep-button type="warning" plain @click="handleDelete(scope.row)">删除</iep-button>
            <iep-button type="warning" plain @click="handleDefault(scope.row)" v-if="scope.row.isDefault === '1'">已默认</iep-button>
            <iep-button type="warning" plain @click="handleDefault(scope.row)" v-else>默认</iep-button>
          </operation-wrapper>
        </template>
      </el-table-column>
    </iep-table>
    <!-- <dialog-system ref="dialogSystem"></dialog-system> -->
    <AddressDialogForm ref="AddressDialogForm" @load-page="loadPage"></AddressDialogForm>
  </div>
</template>

<script>
import { getReceiveAddressList, DeleteReceiveAddressById, CreateReceiveAddress, UpdateReceiveAddress, setReceiveAddressDefault } from '@/api/order'
import Mixin from '@/mixins/mixins'
import operationMixins from '@/mixins/operationMixins'
import { columnsMap } from './options'
import AddressDialogForm from '@/views/modules/product/order/AddressDialogForm.vue'
import { AddressDTO } from '@/views/modules/product/order/options'
export default {
  components: { AddressDialogForm },
  mixins: [operationMixins, Mixin],
  data () {
    return {
      backOption: {
        isBack: false
      },
      columnsMap,
      dictsMap: {}
    }
  },
  mounted () {
    this.isAll = this.$route.query.isAll
  },
  methods: {
    handleCreate () {
      this.$refs.AddressDialogForm.dialogTitle = '新增'
      this.$refs.AddressDialogForm.dialogShow = true
      this.$refs.AddressDialogForm.form = AddressDTO()
      this.$refs.AddressDialogForm.formRequestFn = CreateReceiveAddress
    },
    handleUpdate (row) {
      this.$refs.AddressDialogForm.dialogTitle = '修改'
      this.$refs.AddressDialogForm.dialogShow = true
      this.$refs.AddressDialogForm.form = row
      this.$refs.AddressDialogForm.formRequestFn = UpdateReceiveAddress
    },
    handleDelete (row) {
      this._handleComfirm(row.id, DeleteReceiveAddressById, '删除')
    },
    handleDefault (row) {
      this.$http({
        url: this.$http.adornUrl(setReceiveAddressDefault(row.id).url),
        method: setReceiveAddressDefault().method,
        data: this.form
      }).then(({ data }) => {
        if (data) {
          this.$message.success('操作成功')
          this.loadPage()
        }
      })
    },
    handleSearch (form) {
      this.$refs['CrudTable'].searchPage(form)
    },
    loadPage () {
      this.isLoadTable = true
      this.$http({
        url: this.$http.adornUrl(getReceiveAddressList().url),
        method: getReceiveAddressList().method
      }).then(({ data }) => {
        this.isLoadTable = false
        this.pagedTable = data.data
      })
    }
  },
  created () {
    this.loadPage()
  }
}
</script>
